package team.knowers.qzh.qzhgrame.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.qzh.qzhgrame.common.convention.exception.ClientException;
import team.knowers.qzh.qzhgrame.dao.entity.CourseDO;
import team.knowers.qzh.qzhgrame.dao.mapper.CourseMapper;
import team.knowers.qzh.qzhgrame.dto.req.*;
import team.knowers.qzh.qzhgrame.dto.resp.AllCourseRespDTO;
import team.knowers.qzh.qzhgrame.dto.resp.CourseMessageRespDTO;
import team.knowers.qzh.qzhgrame.dto.resp.CoursePageRespDTO;
import team.knowers.qzh.qzhgrame.service.CourseGrameService;

import java.util.ArrayList;
import java.util.List;

import static team.knowers.qzh.qzhgrame.common.web.FourteenDigitRandomGenerator.generateFourteenDigitRandom;

/**
 * 课程服务实现层
 *
 * @author wt
 */
@Service
@AllArgsConstructor
public class CourseGrameServiceImpl extends ServiceImpl<CourseMapper, CourseDO> implements CourseGrameService {
    /**
     * 创建课程
     */
    @Override
    public int addCourse(AddCourseReqDTO req) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class)
                .eq(CourseDO::getName, req.getName())
                .eq(CourseDO::getUserId, req.getUserId())
                .eq(CourseDO::getDelFlag, 0);
        CourseDO courseDO = baseMapper.selectOne(queryWrapper);
        if (courseDO != null) {
            throw new ClientException("课程已存在");
        }
        long a=generateFourteenDigitRandom();
        CourseDO bean = BeanUtil.toBean(req, CourseDO.class);
        bean.setId(a);
        return baseMapper.insert(bean);
    }

    /**
     * 删除课程
     *
     * @param
     * @return
     */

    @Override
    public int deleteCourse(Long id) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class).eq(CourseDO::getId, id)
                .eq(CourseDO::getDelFlag, 0);
        CourseDO courseDO = baseMapper.selectOne(queryWrapper);
        courseDO.setDelFlag(1);

        return baseMapper.update(courseDO, queryWrapper);
    }

    @Override
    public int updateCourse(UpdateCourseReqDTO req) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class)
                .eq(CourseDO::getId, req.getId())
                .eq(CourseDO::getUserId, req.getUserId())
                .eq(CourseDO::getDelFlag, 0);
        CourseDO courseDO = baseMapper.selectOne(queryWrapper);
        courseDO.setName(req.getName());
        courseDO.setCover(req.getCover());
        courseDO.setSubjectId(req.getSubjectId());
        courseDO.setDescription(req.getDescription());
        LambdaUpdateWrapper<CourseDO> updateWrapper = Wrappers.lambdaUpdate(CourseDO.class)
                .eq(CourseDO::getId, req.getId())
                .eq(CourseDO::getUserId, req.getUserId());
        return baseMapper.update(courseDO, updateWrapper);

    }

    /**
     * 课程审核
     */
    @Override
    public String examineCourse(ExamineCourseReqDTO req) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class)
                .eq(CourseDO::getName, req.getName())
                .eq(CourseDO::getDelFlag, 0);
        CourseDO courseDO = baseMapper.selectOne(queryWrapper);
        courseDO.setStatus(2);
        LambdaUpdateWrapper<CourseDO> queryWrapper1 = Wrappers.lambdaUpdate(CourseDO.class)
                .eq(CourseDO::getName, req.getName())
                .eq(CourseDO::getDelFlag, 0)
                .eq(CourseDO::getStatus, 2);
        baseMapper.update(courseDO, queryWrapper1);
        return (courseDO.getName() + "通过审核");

    }

    /**
     * 课程审核不通过
     */
    @Override
    public String notExamineCourse(ExamineCourseReqDTO req) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class)
                .eq(CourseDO::getName, req.getName())
                .eq(CourseDO::getDelFlag, 0);
        CourseDO courseDO = baseMapper.selectOne(queryWrapper);
        courseDO.setStatus(0);
        LambdaUpdateWrapper<CourseDO> queryWrapper1 = Wrappers.lambdaUpdate(CourseDO.class)
                .eq(CourseDO::getName, req.getName())
                .eq(CourseDO::getDelFlag, 0)
                .eq(CourseDO::getStatus, 0);
        baseMapper.update(courseDO, queryWrapper1);
        return (courseDO.getName() + "未通过审核");
    }

    @Override
    public IPage<CoursePageRespDTO> pageCourse(PageCourseReqDTO req) {
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.lambdaQuery(CourseDO.class).eq(CourseDO::getUserId, req.getUserId())
                .eq(CourseDO::getDelFlag, 0)
                .orderByAsc(CourseDO::getCreateTime);
        IPage<CourseDO> coursePageRespDTOIPage = baseMapper.selectPage(req, queryWrapper);
        return coursePageRespDTOIPage.convert(each -> BeanUtil.toBean(each, CoursePageRespDTO.class));
    }

    @Override
    public CourseMessageRespDTO showCourse(Long id) {
        LambdaQueryWrapper<CourseDO> queryWrapper=Wrappers.lambdaQuery(CourseDO.class)
                .eq(CourseDO::getId,id)
                .eq(CourseDO::getDelFlag,0);
        CourseDO courseDO=baseMapper.selectOne(queryWrapper);
        return BeanUtil.toBean(courseDO,CourseMessageRespDTO.class);
    }

    @Override
    public Integer rsleaseCourse(ReleaseCourseReqDTO req) {
       LambdaQueryWrapper<CourseDO> queryWrapper=Wrappers.lambdaQuery(CourseDO.class)
               .eq(CourseDO::getId,req.getId())
               .eq(CourseDO::getUserId,req.getUserId())
               .eq(CourseDO::getDelFlag,0);
       CourseDO courseDO=baseMapper.selectOne(queryWrapper);
       courseDO.setTrySee(req.getTrySee());
       courseDO.setStatus(1);
       return baseMapper.update(courseDO,queryWrapper);
    }

    @Override
    public List<AllCourseRespDTO> showMajorCourse(Long id) {
        LambdaQueryWrapper<CourseDO> queryWrapper=Wrappers.lambdaQuery(CourseDO.class)
                .eq(CourseDO::getSubjectId,id)
                .eq(CourseDO::getDelFlag,0)
                .orderByAsc(CourseDO::getCreateTime);
        List<CourseDO> courseDOList=baseMapper.selectList(queryWrapper);
        List<AllCourseRespDTO> allCourseRespDTOS=new ArrayList<>();
        for(CourseDO courseDO:courseDOList)
        {
            AllCourseRespDTO courseResp=new AllCourseRespDTO();
            courseResp.setId(courseDO.getId());
            courseResp.setName(courseDO.getName());
            allCourseRespDTOS.add(courseResp);
        }
        return allCourseRespDTOS;
    }

    @Override
    public Integer cancelRsleaseCourse(Long id) {
        LambdaQueryWrapper<CourseDO> queryWrapper=Wrappers.lambdaQuery(CourseDO.class).eq(CourseDO::getId,id)
                .eq(CourseDO::getDelFlag,0);
        CourseDO courseDO=baseMapper.selectOne(queryWrapper);
        courseDO.setStatus(0);
        return baseMapper.update(courseDO,queryWrapper);
    }

}
